/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package im.general;

import jade.core.behaviours.SimpleBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;

/**
 *
 * @author Gnaskky
 */
public abstract class AbstractListenOnceBehaviour extends SimpleBehaviour {

    //TypeBehavioursHandler bhandler;
    protected MessageTemplate mt;
    protected boolean finished = false;

    public void action() {
        ACLMessage msg;

        if(mt==null) {
            msg = myAgent.receive();
        }
        else {
            msg = myAgent.receive(mt);
        }

        if (msg != null) {
            if(Config.TRACE_LVL > Config.NIGHTMARE_TR_LVL )
                System.out.println("[AbstractListenOnceBehaviour] " + myAgent.getAID().getName() + " handling message performative: "+msg.getPerformative());
            handleMessage(msg);
        }
        else
            block();
    }

    //protected abstract void manageMessage(ACLMessage msg);

    protected abstract void handleMessage(ACLMessage msg);
   /*{
    Behaviour b = bhandler.getBehaviour(getMessagePayload(msg).getType());
    // passare msg al behaviour
    myAgent.addBehaviour(b);
    }*/

    public boolean done() {
        return finished;
    }

    protected void setDone() {
        finished = true;
    }

}